package servlet;


import dao.UserDao;
import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;


@WebServlet(urlPatterns = "/DeleteUserServlet.do")

public class DeleteUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, IOException {
        // 获取要删除的用户ID
//        int userId = Integer.parseInt(request.getParameter("userId"));\
        UserDao userDao=new UserDao();
        String idString = request.getParameter("id");
        if (idString != null && !idString.isEmpty()) {
            int id = Integer.parseInt(idString);
            boolean success2=userDao.delete(id);
            if(success2){
                response.sendRedirect("background1/admin/usermanager.jsp");
            }else {
                response.sendRedirect("home/404.html");
            }

        } else {
            response.sendRedirect("home/404.html");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    private boolean is_exit(int userId) {

        JDBCUtil jdbcUtil = new JDBCUtil();

        try {
            String sql = "SELECT * FROM 02_38_yangyang WHERE username = ?";
            ResultSet resultSet = jdbcUtil.prepareQuery(sql, userId);
            if (resultSet.next()) {
                return true;
            }
        }
            // 处理数据库异常
        catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            jdbcUtil.closeConnection(); // 关闭数据库连接
        }

       return false;
    }






}

